import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { usePermissStore } from '../store/permiss';
import Home from '../views/home.vue';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/login',
    },
    {
        path: '/',
        name: 'Home',
        component: Home,
        children: [
            {
                path: '/dashboard',
                name: 'dashboard',
                meta: {
                    title: '系统首页',
                    noAuth: true,
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/dashboard.vue'),
            },
            // 合作管理员
            {
                path: '/collaborativeAdministrator-user',
                name: 'collaborativeAdministrator-user',
                meta: {
                    title: '合作管理员',
                    permiss: '11',
                },
                component: () => import(/* webpackChunkName: "collaborativeAdministrator-user" */ '../views/collaborativeAdministratorManage/user.vue'),
            },
            {
                path: '/collaborativeAdministrator-marketSubsidies',
                name: 'collaborativeAdministrator-marketSubsidies',
                meta: {
                    title: '市场补贴',
                    permiss: '12',
                },
                component: () => import(/* webpackChunkName: "collaborativeAdministrator-marketSubsidies" */ '../views/collaborativeAdministratorManage/marketSubsidies.vue'),
            },
            {
                path: '/collaborativeAdministrator-marketRrevenue',
                name: 'collaborativeAdministrator-marketRrevenue',
                meta: {
                    title: '市场收益',
                    permiss: '13',
                },
                component: () => import(/* webpackChunkName: "collaborativeAdministrator-marketRrevenue" */ '../views/collaborativeAdministratorManage/marketRrevenue.vue'),
            },
            {
                path: '/collaborativeAdministrator-role',
                name: 'collaborativeAdministrator-role',
                meta: {
                    title: '合作管理员角色配置',
                    permiss: '16',
                },
                component: () => import(/* webpackChunkName: "collaborativeAdministrator-role" */ '../views/collaborativeAdministratorManage/role.vue'),
            },
            // 场所管理
            {
                path: '/venueManagement-place',
                name: 'venueManagement-place',
                meta: {
                    title: '场地管理',
                    permiss: '21',
                },
                component: () => import(/* webpackChunkName: "venueManagement-place" */ '../views/venueManagement/place.vue'),
            },
            {
                path: '/venueManagement-venueSubsidy',
                name: 'venueManagement-venueSubsidy',
                meta: {
                    title: '场地补贴',
                    permiss: '22',
                },
                component: () => import(/* webpackChunkName: "venueManagement-venueSubsidy" */ '../views/venueManagement/venueSubsidy.vue'),
            },
            {
                path: '/venueManagement-cabinet',
                name: 'venueManagement-cabinet',
                meta: {
                    title: '机柜设备',
                    permiss: '23',
                },
                component: () => import(/* webpackChunkName: "venueManagement-cabinet" */ '../views/venueManagement/cabinet.vue'),
            },
            {
                path: '/venueManagement-cabinetMonitor',
                name: 'venueManagement-cabinetMonitor',
                meta: {
                    title: '设备监控',
                    permiss: '26',
                },
                component: () => import(/* webpackChunkName: "venueManagement-cabinetMonitor" */ '../views/venueManagement/cabinetMonitor.vue'),
            },
            {
                path: '/venueManagement-CabinetPrmSet',
                name: 'venueManagement-CabinetPrmSet',
                meta: {
                    title: '机柜参数设置',
                    permiss: '23',
                },
                component: () => import(/* webpackChunkName: "venueManagement-CabinetPrmSet" */ '../views/venueManagement/CabinetPrmSet.vue'),
            },
            {
                path: '/venueManagement-venueRevenue',
                name: 'venueManagement-venueRevenue',
                meta: {
                    title: '场地收益',
                    permiss: '24',
                },
                component: () => import(/* webpackChunkName: "venueManagement-venueRevenue" */ '../views/venueManagement/venueRevenue.vue'),
            },
            {
                path: '/venueManagement-equipmentOperationRecords',
                name: 'venueManagement-equipmentOperationRecords',
                meta: {
                    title: '设备运行记录',
                    permiss: '25',
                },
                component: () => import(/* webpackChunkName: "venueManagement-equipmentOperationRecords" */ '../views/venueManagement/equipmentOperationRecords.vue'),
            },
            // 眼镜管理
            {
                path: '/glassManagement-warehousing',
                name: 'glassManagement-warehousing',
                meta: {
                    title: '眼镜入库',
                    permiss: '31',
                },
                component: () => import(/* webpackChunkName: "glassManagement-warehousing" */ '../views/glassManagement/warehousing.vue'),
            },
            {
                path: '/glassManagement-info',
                name: 'glassManagement-info',
                meta: {
                    title: '眼镜信息',
                    permiss: '32',
                },
                component: () => import(/* webpackChunkName: "glassManagement-info" */ '../views/glassManagement/info.vue'),
            },
            {
                path: '/glassManagement-classification',
                name: 'glassManagement-classification',
                meta: {
                    title: '眼镜分类',
                    permiss: '33',
                },
                component: () => import(/* webpackChunkName: "glassManagement-classification" */ '../views/glassManagement/classification.vue'),
            },
            {
                path: '/glassManagement-record',
                name: 'glassManagement-record',
                meta: {
                    title: '眼镜使用记录',
                    permiss: '34',
                },
                component: () => import(/* webpackChunkName: "glassManagement-record" */ '../views/glassManagement/record.vue'),
            },
            // 眼镜订单
            {
                path: '/galssOrder-orderInfo',
                name: 'galssOrder-orderInfo',
                meta: {
                    title: '订单信息',
                    permiss: '41',
                },
                component: () => import(/* webpackChunkName: "galssOrder-orderInfo" */ '../views/galssOrder/orderInfo.vue'),
            },
            {
                path: '/galssOrder-orderInfoDetail',
                name: 'galssOrder-orderInfoDetail',
                meta: {
                    title: '订单详情',
                    permiss: '41',
                },
                component: () => import(/* webpackChunkName: "galssOrder-orderInfoDetail" */ '../views/galssOrder/orderInfoDetail.vue'),
            },
            {
                path: '/galssOrder-orderDetail',
                name: 'galssOrder-orderDetail',
                meta: {
                    title: '借还详情',
                    permiss: '41',
                },
                component: () => import(/* webpackChunkName: "galssOrder-orderDetail" */ '../views/galssOrder/orderDetail.vue'),
            },
            {
                path: '/galssOrder-BRRecords',
                name: 'galssOrder-BRRecords',
                meta: {
                    title: '眼镜借还记录',
                    permiss: '42',
                },
                component: () => import(/* webpackChunkName: "galssOrder-info" */ '../views/galssOrder/BRRecords.vue'),
            },
            {
                path: '/galssOrder-depositReview',
                name: 'galssOrder-depositReview',
                meta: {
                    title: '押金待退换审核',
                    permiss: '43',
                },
                component: () => import(/* webpackChunkName: "galssOrder-depositReview" */ '../views/galssOrder/depositReview.vue'),
            },
            {
                path: '/galssOrder-depositReviewRecord',
                name: 'galssOrder-depositReviewRecord',
                meta: {
                    title: '押金退还记录',
                    permiss: '44',
                },
                component: () => import(/* webpackChunkName: "galssOrder-depositReviewRecord" */ '../views/galssOrder/depositReviewRecord.vue'),
            },
            // 租户管理
            {
                path: '/tenantManagement-tenantInfo',
                name: 'tenantManagement-tenantInfo',
                meta: {
                    title: '租户信息',
                    permiss: '51',
                },
                component: () => import(/* webpackChunkName: "tenantManagement-tenantInfo" */ '../views/tenantManagement/tenantInfo.vue'),
            },
            // 支付记录
            {
                path: '/payRecord-payList',
                name: 'payRecord-payList',
                meta: {
                    title: '支付记录',
                    permiss: '61',
                },
                component: () => import(/* webpackChunkName: "galssOrder-payList" */ '../views/payRecord/payList.vue'),
            },

            // 用户指南
            {
                path: '/usrGuide-column',
                name: 'usrGuide-column',
                meta: {
                    title: '指南栏目',
                    permiss: '71',
                },
                component: () => import(/* webpackChunkName: "usrGuide-payList" */ '../views/usrGuide/column.vue'),
            },
            {
                path: '/usrGuide-article',
                name: 'usrGuide-article',
                meta: {
                    title: '指南文章',
                    permiss: '72',
                },
                component: () => import(/* webpackChunkName: "usrGuide-article" */ '../views/usrGuide/article.vue'),
            },
            // 用户管理
            {
                path: '/userMagement-user',
                name: 'userMagement-user',
                meta: {
                    title: '用户管理',
                    permiss: '111',
                },
                component: () => import(/* webpackChunkName: "userMagement-user" */ '../views/userMagement/user.vue'),
            },
            {
                path: '/userMagement-role',
                name: 'userMagement-role',
                meta: {
                    title: '角色管理',
                    permiss: '112',
                },
                component: () => import(/* webpackChunkName: "userMagement-role" */ '../views/userMagement/role.vue'),
            },
            // 广告管理
            {
                path: '/adMagement-ad',
                name: 'adMagement-ad',
                meta: {
                    title: '指南栏目',
                    permiss: '71',
                },
                component: () => import(/* webpackChunkName: "adMagement-ad" */ '../views/adMagement/ad.vue'),
            },
            {
                path: '/adMagement-word',
                name: 'adMagement-word',
                meta: {
                    title: '指南文章',
                    permiss: '72',
                },
                component: () => import(/* webpackChunkName: "adMagement-word" */ '../views/adMagement/word.vue'),
            },

            // 统计分析-
            {
                path: '/statisticalAnalysis-comprehensive',
                name: 'statisticalAnalysis-comprehensive',
                meta: {
                    title: '综合数据分析',
                    permiss: '81',
                },
                component: () => import(/* webpackChunkName: "statisticalAnalysis-allData" */ '../views/statisticalAnalysis/allData.vue'),
            },
            // 统计分析-押金营销数据
            {
                path: '/statisticalAnalysis-marketing',
                name: 'statisticalAnalysis-marketing',
                meta: {
                    title: '押金营销数据',
                    permiss: '82',
                },
                component: () => import(/* webpackChunkName: "statisticalAnalysis-marketing" */ '../views/statisticalAnalysis/marketing.vue'),
            },
             // 意见建议
             {
                path: '/suggestion-complain',
                name: 'suggestion-complain',
                meta: {
                    title: '投诉与建议',
                    permiss: '91',
                },
                component: () => import(/* webpackChunkName: "suggestion-complain" */ '../views/suggestion/complain.vue'),
            },



            {
                path: '/icon',
                name: 'icon',
                meta: {
                    title: '图标',
                    permiss: '5',
                },
                component: () => import(/* webpackChunkName: "icon" */ '../views/pages/icon.vue'),
            },
            {
                path: '/ucenter',
                name: 'ucenter',
                meta: {
                    title: '个人中心',
                },
                component: () => import(/* webpackChunkName: "ucenter" */ '../views/pages/ucenter.vue'),
            },
        ],
    },
    {
        path: '/login',
        meta: {
            title: '登录',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "login" */ '../views/pages/login.vue'),
    },
    {
        path: '/reset-pwd',
        meta: {
            title: '重置密码',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "reset-pwd" */ '../views/pages/reset-pwd.vue'),
    },
    {
        path: '/403',
        meta: {
            title: '没有权限',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "403" */ '../views/pages/403.vue'),
    },
    {
        path: '/404',
        meta: {
            title: '找不到页面',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "404" */ '../views/pages/404.vue'),
    },
    { path: '/:path(.*)', redirect: '/404' },
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    NProgress.start();
    const userInfo = localStorage.getItem('userInfo')
    if(!userInfo) {
        if (to.path.indexOf('login') < 0) {
            next(`/login?url=${to.path}`);
        } else {
            next()
        }
    } else {
        next();
    }
    // const role = localStorage.getItem('vuems_name');
    // const permiss = usePermissStore();

    // if (!role && to.meta.noAuth !== true) {
    //     next('/login');
    // } else if (typeof to.meta.permiss == 'string' && !permiss.key.includes(to.meta.permiss)) {
    //     // 如果没有权限，则进入403
    //     next('/403');
    // } else {
    //     next();
    // }
});

router.afterEach(() => {
    NProgress.done();
});

export default router;
